﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="PrincipalClaim", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(PrincipalClaim))]
    public class PrincipalClaimSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IPrincipalClaim clazz;
        
        public PrincipalClaimSerializer(IPrincipalClaim clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("ClaimID");
            if (clazz.ClaimID!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "NonEmptyString");
                Writer.WriteString(clazz.ClaimID.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ClaimID");
            }
            Writer.WriteEndElement();
            for (int x = 0; ((clazz.CollateralClaim != null) 
                        && (x < clazz.CollateralClaim.Count)); x = (x + 1)) {
                if (clazz.CollateralClaim[x]!=null) {
                    Writer.WriteStartElement("CollateralClaim");
                    global::Xtee.Core.SerializationMetadata metaCollateralClaim = this.GetMetaData(clazz.CollateralClaim[x]);
                    Writer.WriteTypeAttribute(metaCollateralClaim.TypeQualifiedName.Namespace, metaCollateralClaim.TypeQualifiedName.Name);
                    metaCollateralClaim.NewSerializer(clazz.CollateralClaim[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            Writer.WriteStartElement("PrincipalClaimSum");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "decimal");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.PrincipalClaimSum))).ToString());
            Writer.WriteEndElement();
            if (((clazz.Dept == null) 
                        || (clazz.Dept.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "Dept");
            }
            for (int x = 0; ((clazz.Dept != null) 
                        && (x < clazz.Dept.Count)); x = (x + 1)) {
                Writer.WriteStartElement("Dept");
                if (clazz.Dept[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaDept = this.GetMetaData(clazz.Dept[x]);
                    Writer.WriteTypeAttribute(metaDept.TypeQualifiedName.Namespace, metaDept.TypeQualifiedName.Name);
                    metaDept.NewSerializer(clazz.Dept[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Dept");
                }
                Writer.WriteEndElement();
            }
            Writer.WriteStartElement("ClaimBase");
            if (clazz.ClaimBase!=null) {
                global::Xtee.Core.SerializationMetadata metaClaimBase = this.GetMetaData(clazz.ClaimBase);
                Writer.WriteTypeAttribute(metaClaimBase.TypeQualifiedName.Namespace, metaClaimBase.TypeQualifiedName.Name);
                metaClaimBase.NewSerializer(clazz.ClaimBase).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ClaimBase");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="ClaimID") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PrincipalClaim>ClaimID");
                }
                clazz.ClaimID = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.NonEmptyString(Reader.ReadElementString("ClaimID"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>PrincipalClaim>ClaimID", Reader.LocalName);
            }
            if (Reader.LocalName=="CollateralClaim") {
                clazz.CollateralClaim = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.ICollateralClaim>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "CollateralClaim")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PrincipalClaim>CollateralClaim");
                    }
                    global::Xtee.Core.SerializationMetadata metaCollateralClaim = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.ICollateralClaim>(Reader, "CollateralClaim", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.ICollateralClaim item = metaCollateralClaim.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.ICollateralClaim>();
                    clazz.CollateralClaim.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("CollateralClaim");
                        metaCollateralClaim.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="PrincipalClaimSum") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PrincipalClaim>PrincipalClaimSum");
                }
                clazz.PrincipalClaimSum = new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("PrincipalClaimSum")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>PrincipalClaim>PrincipalClaimSum", Reader.LocalName);
            }
            clazz.Dept = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "Dept")); ) {
                if (Reader.LocalName=="Dept") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PrincipalClaim>Dept");
                    }
                    global::Xtee.Core.SerializationMetadata metaDept = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept>(Reader, "Dept", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept item = metaDept.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDept>();
                    clazz.Dept.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("Dept");
                        metaDept.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>PrincipalClaim>Dept", Reader.LocalName);
                }
            }
            if ((clazz.Dept.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "Dept");
            }
            if (Reader.LocalName=="ClaimBase") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>PrincipalClaim>ClaimBase");
                }
                global::Xtee.Core.SerializationMetadata metaClaimBase = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaimBasePrincipalClaim>(Reader, "ClaimBase_PrincipalClaim", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.ClaimBase = metaClaimBase.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IClaimBasePrincipalClaim>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ClaimBase");
                    metaClaimBase.NewSerializer(clazz.ClaimBase).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>PrincipalClaim>ClaimBase", Reader.LocalName);
            }
        }
    }
}
